﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using OpenSoftStore.Orm;
using OpenSoftStore.Orm.Model;
using System.Linq;
using OpenSoftStore.Orm.View;
using System.Data;

namespace OpenSoftStore.BusinessLogic.Admin
{

    public class AdminLogic : ABusinessLogic
    {
        public T_Tree Tree;

        public AdminLogic()
        {
            Tree=new T_Tree();
        }

        public int SaveAdmin(T_Admin admin,List<T_AdminPowerMapp> adminPowerMapps)
        {
            bool add = true;
            if (string.IsNullOrEmpty(admin.Id))
            {
                admin.Id = Guid.NewGuid().ToString("D");
            }
            else
            {
                var adminPowerMappDeletes = Spa.Where<T_AdminPowerMapp>(m => m.AdminId == admin.Id);
                Spa.DeleteObjects(adminPowerMappDeletes);
                Spa.SaveChanges();
                add = false;
            }
            foreach (var adminPowerMapp in adminPowerMapps)
            {
                adminPowerMapp.AdminId = admin.Id;
            }

            if (add) Spa.AddObject(admin);
            else Spa.UpdateObject(admin);
            Spa.AddObjects(adminPowerMapps);
            int row = Spa.SaveChanges();
            return row;
        }

        public int SaveRole(T_Tree tree, List<T_AdminPowerMapp> adminPowerMapps)
        {
            bool add = true;
            if (string.IsNullOrEmpty(tree.Id))
            {
                tree.Id = Guid.NewGuid().ToString("D");
                tree.RootType = "AdminTree";
                var parentTree = Spa.Where<T_Tree>(m => m.Id == tree.ParentId).FirstOrDefault();
                var count = Spa.Where<T_Tree>(m => m.ParentId == tree.ParentId).Count();
                if (parentTree != null)
                {
                    tree.Code = parentTree.Code + count + ".";
                }
            }
            else
            {
                var adminPowerMappDeletes = Spa.Where<T_AdminPowerMapp>(m => m.AdminId == tree.Id);
                Spa.DeleteObjects(adminPowerMappDeletes);
                Spa.SaveChanges();
                add = false;
            }
            foreach (var adminPowerMapp in adminPowerMapps)
            {
                adminPowerMapp.AdminId = tree.Id;
            }

            if (add) Spa.AddObject(tree);
            else Spa.UpdateObject(tree);
            Spa.AddObjects(adminPowerMapps);
            int row = Spa.SaveChanges();
            return row;
        }
        #region Methods

        #region Admin
        public IEnumerable<T_AdminPowerMapp> GetAdminPowerMapps(string strWhere = "")
        {
            return Spa.Where<T_AdminPowerMapp>(strWhere);
        }

        public IEnumerable<V_Admin> GetAdminView(string strWhere = "", string strOrderBy="")
        {
            string sql = "select T_Admin.*,T_Tree.Name as RoleName,T_Shop.Name as ShopName from (T_Admin inner join T_Tree on T_Admin.TreeId=T_Tree.Id)  "
                         + "inner join T_Shop on T_Admin.ShopId=T_Shop.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            if (!String.IsNullOrEmpty(strOrderBy)) sql += strOrderBy;
            else sql += "Order By T_Admin.AddTime asc";

            return Spa.SelectWhere<V_Admin>(sql);
        }

        public IEnumerable<T_Tree> GetRoles(string strWhere = "")
        {
            return Spa.SelectWhere<T_Tree>("select * from T_tree " + strWhere);
        }

        public IEnumerable<T_Shop> GetShops(string strWhere = "")
        {
            return Spa.SelectWhere<T_Shop>(
                "select * from T_Shop " + strWhere
                );
        }


        public DataTable GetAdminViewFast(string strWhere = "", string strOrderBy="")
        {
            string sql = " select T_Admin.*,T_Tree.Name as RoleName,T_Shop.Name as ShopName from ((T_Admin inner join T_Tree on T_Admin.TreeId=T_Tree.Id)  inner join T_Shop on T_Admin.ShopId=T_Shop.Id)  ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            if (!String.IsNullOrEmpty(strOrderBy)) sql += strOrderBy;
            else sql += "Order By T_Admin.AddTime asc";
            var data = Spa.RunSqLcmdDataSet(sql);
            if (data == null) return new DataTable();
            return data.Tables[0];
        }

        public IEnumerable<T_Admin> GetAdminList()
        {
            return Spa.SelectWhere<T_Admin>("select Id,Name from T_Admin ");
        }
        #endregion

        #endregion
    }
}

